<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.ort">
<head>
    <meta charset="UTF-8">
    <title>优惠卷查询</title>
    <script th:src="@{/layui/layui.js}" type="text/javascript"></script>
    <link th:href="@{/layui/css/layui.css}" rel="stylesheet"/>
</head>
<body>
<div class="layui-panel" style="padding-top: 10px">
    <fieldset class="layui-elem-field layui-border-blue">
        <legend>搜索信息</legend>
        <form class="layui-form layui-form-pane" style="margin: 20px;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" type="text" name="name" autocomplete="off" />
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <a class="layui-btn layui-btn-primary" lay-submit="" lay-filter="searchBtn">
                            <i class="layui-icon layui-icon-search"></i>
                            搜索
                        </a>
                    </div>
                </div>
            </div>

        </form>
    </fieldset>
    <div id="btn" style="display: none">
        {{#if(d.status==1){}}
        <input class="{{d.id}}" type="checkbox" value="{{d.status}}" lay-filter="status"  lay-skin="switch"  lay-text="可用|不可用" checked>
       {{#}}}
        {{#if(d.status==0){}}
        <input class="{{d.id}}"  type="checkbox" value="{{d.status}}" lay-filter="status"  lay-skin="switch"  lay-text="可用|不可用">
       {{#}}}
    </div>
    <div>
     <table id="couponsTable" lay-filter="couponsTable"></table>
    </div>

    <div id="toolbar" style="display: none">
        <a class="layui-btn layui-bg-blue" lay-event="add">添加
        <i class="layui-icon layui-icon-add-1"></i>
        </a>
    </div>
    <div id="tool" style="display: none">
        <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="edit">编辑
            <i class="layui-icon layui-icon-add-1"></i>
        </a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除
            <i class="layui-icon layui-icon-delete"></i>
        </a>
    </div>

</div>

<script>
    var $;
    layui.use(['jquery','table','form'],function (){
        $ = layui.jquery;
        var table = layui.table,form = layui.form,layer=layui.layer;

        table.render({
            elem:'#couponsTable',
            height:'full-160',
            url:'/coupons/cs/find',
            toolbar:'#toolbar',
            page:true,
            cols:
            [
                [
                    {fixed:'left',type:"numbers",title:'编号',width:50},
                    {field:"name",title: "优惠卷名称"},
                    {field:"minimum",title: "最低消费"},
                    {field:"reduction",title: "满减金额"},
                    {field:"eachLimitGet",title: "每人限领"},
                    {field:"type",title: "类型"},
                    {field:"number",title: "优惠劵数量"},
                    {field:"status",title: "可用状态",templet:'#btn'},
                    {fixed:'right',title:'操作',templet:'#tool'}

                ]
            ]


        })



        form.on('switch(status)',function (obj){
            var value;
           var id = $(this).attr('class');

           if (obj.value==1){
               value = 0
           }else {
               value = 1
           }

            $.ajax({
                type:'post',
                url:'/coupons/cs/save',
                data:{
                    id:id,
                    status:value
                },
                success:function (res){
                    table.reload('couponsTable');
                }

            })
        })
        table.on('toolbar(couponsTable)',function (data){
            if (data.event=='add'){
                layer.open({
                    type:2,
                    content:'/page/coupons/couponsAdd',
                    area:['700px','600px'],
                    btn:['确定','取消'],
                    btn1:function (index,o){
                        o.find('iframe')[0].contentWindow.clickSubmit();

                    }
                })
            }
        })
        table.on('tool(couponsTable)',function (data){

            if (data.event=='edit'){
               layer.open({
                    type:2,
                    content:'/page/coupons/couponsEdit?id='+data.data.id,
                    area:['700px','600px'],
                    btn:['确定','取消'],
                    btn1:function (index,o){
                        o.find('iframe')[0].contentWindow.clickSubmit();
                        return false;
                    }
                })
            }else if (data.event=='delete'){
                $.ajax({
                    type:'get',
                    url: '/coupons/cs/delete?id='+data.data.id,
                    dataType:'json',
                    success:function (res){
                        layer.msg(res.msg);
                        table.reload('couponsTable');
                    }
                })
            }
        })

        form.on('submit(searchBtn)',function (obj){

            table.reload('couponsTable',{
                where: {
                    name:obj.field.name
                }, //设定异步数据接口的额外参数
                    page: {
                    curr: 1 //重新从第 1 页开始
                }

            });
        })

    })
</script>
</body>
</html>